package com.sockerConfig;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.logging.Logger;

@Component
public class WebSocketHandler extends TextWebSocketHandler {

  private Logger logger = Logger.getLogger(WebSocketHandler.class.getName());

  @Override
  public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
    logger.info("Sending message: " + message.getPayload() + " to " +  " sessions.");
    String payload = message.getPayload();

    System.out.printf("payload: %1s", payload);

  }

  @Override
  public void afterConnectionEstablished(WebSocketSession session) {
    logger.info("Added Websocket session, total number of sessions are " );
  }

  @Override
  public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    logger.info("Removed Websocket session, total number of sessions are ");
  }

}